﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace DreamBox.Net.Core
{
    public static class Transform
    {
        /// <summary>
        /// 转换为Decimal型
        /// </summary>
        /// <param name="inputText"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal GetDecimal(string inputText, decimal defaultValue)
        {
            if (inputText.Trim() != "")
            {
                Regex r = new Regex(@"^\d+(\.\d+)?$");
                if (r.Match(inputText.Trim()).Success)
                {
                    return decimal.Parse(inputText.Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            else
            {
                return defaultValue;
            }
        }
        /// <summary>
        /// 转换为Int64型
        /// </summary>
        /// 
        /// <param name="inputText"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Int64 GetInt64(string inputText, Int64 defaultValue)
        {
            if (inputText.Trim() != "")
            {
                Regex r = new Regex("^[0-9]*$");
                if (r.Match(inputText.Trim()).Success)
                {
                    return Int64.Parse(inputText.Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            else
            {
                return defaultValue;
            }
        }
        /// <summary>
        /// 转换为int型
        /// </summary>
        /// <param name="inputText"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetInt(string inputText,int defaultValue)
        {
            if (inputText.Trim() != "")
            {
                Regex r = new Regex("^[0-9]*$");
                if (r.Match(inputText.Trim()).Success)
                {
                    return int.Parse(inputText.Trim());
                }
                else
                {
                    return defaultValue;
                }
            }
            else
            {
                return defaultValue;
            }
        }
    }
}
